﻿package at.geoathome.ui.buttons
{
	import at.geoathome.ui.buttons.*;
	import at.geoathome.utils.lang.IMultiLanguageReceiver;
	
	import flash.text.*;
	
	
	/**
	* ...
	* @author Georg Kamptner
	*/
	public class TextButton extends Button implements IMultiLanguageReceiver
	{
		protected var _label_tf:TextField;
		protected var _label_format:TextFormat;
		protected var _label_font:Font;
		
		public function TextButton(button_bg_classname:String = Button.INVISIBLE_BACKGROUND) 
		{
			super(button_bg_classname);
		}
		
		public override function set width(width:Number):void
		{
			super.width = width;
			updateLabelPositionAndSize();
		}
		
		public override function set height(height:Number):void
		{
			super.height = height;
			updateLabelPositionAndSize();
		}
		
		public function addLabel(text:String, color:Number = 0xffffff, font_size:uint = 14, font:Class = null, bold:Boolean = false, filters:Array = null):void
		{
			// remove old label if necessary
			if (_label_tf != null) {
				this.removeChild(_label_tf);
				_label_tf = null;
			}
			
			/// register font
			if (font != null)
			{
				Font.registerFont(font);
				_label_font = new font() as Font;
			}
			
			// create textfield
			_label_tf = new TextField();
			_label_tf.background 	= false;
			_label_tf.border 		= false;
			if (font != null) _label_tf.embedFonts 	= true;
			_label_tf.selectable 	= false;
			if (font != null) _label_tf.antiAliasType = AntiAliasType.ADVANCED;
			_label_tf.sharpness 	= 0;
			_label_tf.thickness 	= 0;
			_label_tf.text 			= text;
			_label_tf.width 		= 10;
			_label_tf.height 		= 10;
			_label_tf.autoSize 		= TextFieldAutoSize.LEFT;
			_label_tf.mouseEnabled  = false;
			
			_label_format = new TextFormat();
			if (font != null) _label_format.font 		= _label_font.fontName;
			_label_format.size 		= font_size;
			if (font != null) _label_format.kerning 	= true;
			_label_format.color 	= color;
			_label_format.bold 		= bold;
			
			_label_tf.defaultTextFormat = _label_format ;
			_label_tf.setTextFormat(_label_format );
			
			// tf size & position
			this.updateLabelPositionAndSize();
			
			// apply filters
			_label_tf.filters = filters;
			
			// add to stage
			this.addChild(_label_tf);
		}
		
		protected function updateLabelPositionAndSize():void
		{
			if (_label_tf == null) return;
			
			// reset textfield "size" for the size calculation is applied on the full size textfield
			_label_tf.wordWrap 	= false;
			_label_tf.autoSize  = TextFieldAutoSize.LEFT;
			_label_tf.multiline = false;
			
			if (_label_tf.width > _bg.width)
			{ // multiline tf
				// tf
				_label_tf.autoSize 	= TextFieldAutoSize.LEFT;
				_label_tf.width 	= _bg.width - 4;
				_label_tf.multiline = true;
				_label_tf.wordWrap 	= true;
				
				// format
				_label_format.align = TextFormatAlign.LEFT;
				_label_tf.defaultTextFormat = _label_format;
				_label_tf.setTextFormat(_label_format);
				
				// set textfield position
				_label_tf.x = (int)((_bg.width - _label_tf.width) / 2);
				_label_tf.y = (int)((_bg.height - _label_tf.height) / 2);
			}
			else
			{ // single line tf
				// tf
				_label_tf.wordWrap 	= false;
				_label_tf.autoSize = TextFieldAutoSize.LEFT;
				
				// format
				_label_format.align = TextFormatAlign.CENTER;
				_label_tf.defaultTextFormat = _label_format;
				_label_tf.setTextFormat(_label_format);
				
				// set textfield position
				_label_tf.x = (int)((_bg.width - _label_tf.width) / 2);
				_label_tf.y = (int)((_bg.height - _label_tf.height) / 2);
			}
		}
		
		
		// com.kronomy.utils.lang.IMultiLanguageReceiver.as :: text(value:String):void
		public function set text(value:String):void
		{
			if (_label_tf == null) {
				addLabel(value);
			} else {
				_label_tf.text = value;
				updateLabelPositionAndSize();
			}
				
		}
		
		public function get text():String
		{
			if (_label_tf == null) {
				return "";
			} else {
				return _label_tf.text;
			}
				
		}
	}
	
}